package com.tubeonfire.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.youtube.UserEventEntry;
import com.google.gdata.data.youtube.UserEventFeed;
import com.google.gdata.util.ServiceException;
import com.google.gdata.util.ServiceForbiddenException;

public class YoutubeActiveService {

	private static YouTubeService service = new YouTubeService(
			"tubeilike",
			"AI39si4hdZVHF2A5UZmnr1yRZ3mWhwQ9lIqfBggC66zeM3qdfUB1DYAdckeqd6IB24-1WGeCZRKh5KruMxYF9cmcBdq9ymU6IQ");
	private static final Logger log = Logger
			.getLogger(YoutubeActiveService.class.getName());
	private static boolean tooManyRecentCallError = false;
	private int limit = 12;
	private List<UserEventEntry> listResult = new ArrayList<UserEventEntry>();

	public List<UserEventEntry> getListResult() {
		return listResult;
	}

	public void setListResult(List<UserEventEntry> listResult) {
		this.listResult = listResult;
	}

	public static void setTooManyRecentCallError(boolean tooManyRecentCallError) {
		YoutubeActiveService.tooManyRecentCallError = tooManyRecentCallError;
	}

	public boolean isTooManyRecentCallError() {
		return tooManyRecentCallError;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getLimit() {
		return limit;
	}

	public void getActiveByChannel(String channel) {
		try {
			listResult = new ArrayList<UserEventEntry>();
			String feedUrl = "http://gdata.youtube.com/feeds/api/events?author="
					+ channel;
			UserEventFeed activityFeed = service.getFeed(new URL(feedUrl),
					UserEventFeed.class);

			for (UserEventEntry entry : activityFeed.getEntries()) {
				String user = entry.getAuthors().get(0).getName();
				if (entry.getUserEventType() == UserEventEntry.Type.VIDEO_UPLOADED) {
					listResult.add(entry);
					System.out.println(user + " uploaded a video "
							+ entry.getVideoId());
				} else if (entry.getUserEventType() == UserEventEntry.Type.VIDEO_RATED) {
					System.out.println(user + " rated a video "
							+ entry.getVideoId() + " "
							+ entry.getRating().getValue() + " stars");
				} else if (entry.getUserEventType() == UserEventEntry.Type.VIDEO_FAVORITED) {
					System.out.println(user + " favorited a video "
							+ entry.getVideoId());
				} else if (entry.getUserEventType() == UserEventEntry.Type.VIDEO_SHARED) {
					listResult.add(entry);
					System.out.println(user + " shared a video "
							+ entry.getVideoId());
				} else if (entry.getUserEventType() == UserEventEntry.Type.VIDEO_COMMENTED) {
					System.out.println(user + " commented on video "
							+ entry.getVideoId());
				} else if (entry.getUserEventType() == UserEventEntry.Type.USER_SUBSCRIPTION_ADDED) {
					System.out.println(user + " subscribed to the channel of "
							+ entry.getUsername());
				} else if (entry.getUserEventType() == UserEventEntry.Type.FRIEND_ADDED) {
					System.out.println(user + " friended "
							+ entry.getUsername());
				}
			}
			tooManyRecentCallError = false;
		} catch (MalformedURLException e) {
			log.warning(e.toString());
			e.printStackTrace();
		} catch (IOException e) {
			log.warning(e.toString());
			e.printStackTrace();
		} catch (ServiceForbiddenException e) {
			log.warning(e.toString());
			e.printStackTrace();
			tooManyRecentCallError = true;
		} catch (ServiceException e) {
			log.warning(e.toString());
			e.printStackTrace();
		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace();
		}
	}
}
